import Vue from "vue";
import VueRouter from "vue-router";
// 主页面
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
// 子页面
import Welcome from "../components/Welcome";
const Users = () => import("../components/users/Users"); //用户信息

const AddGood = () => import("../components/goods/AddGood"); //添加商品
const Order = () => import("../components/order/Order"); //订单管理
const Report = () => import("../components/report/Report"); //报表
Vue.use(VueRouter);

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: Welcome,
      },
      {
        path: "/users",
        component: Users,
      },
      {
        path: "/goods/add",
        component: AddGood,
      },
      {
        path: "/orders",
        component: Order,
      },
      {
        path: "/reports",
        component: Report,
      },
    ],
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  if (!sessionStorage.getItem("token")) return next("/login");
  next();

  // const tokenStr = sessionStorage.getItem('token')
  // if (!tokenStr) return next('/login')
  // next()
});
export default router;
